package org.example;

import cn.hserver.core.server.util.JsonResult;
import cn.hserver.plugin.rpc.annotation.Resource;
import cn.hserver.plugin.web.annotation.Controller;
import cn.hserver.plugin.web.annotation.GET;
import cn.hserver.plugin.web.annotation.POST;
import cn.hserver.plugin.web.context.PartFile;
import cn.hserver.plugin.web.interfaces.HttpRequest;
import cn.hserver.plugin.web.interfaces.HttpResponse;

import java.io.File;

@Controller
public class TestController {

    @Resource(serverName = "user", groupName = "USER_SERVICE")
    private UserService userService;

    @GET("/test")
    private String user() {
        try {
            return userService.user().get();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @POST("/upload")
    private JsonResult user(HttpRequest request, HttpResponse response) {
        PartFile file = request.queryFile("file");
        System.out.println(file.getFileName());
        file.deleteTempCacheFile();
        return JsonResult.ok();
    }

    @GET("/down")
    private void down(HttpRequest request, HttpResponse response) {
        response.setDownloadFile(new File("/Users/heixiaoma/Downloads/11804591_doc.hserver.top_nginx.zip"));
    }

    @GET("/test3")
    private String test3() {
        return "test";
    }

}
